﻿namespace ExamV5.StudyInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmEditPlan : Page
    {
        protected Button butClose;
        protected Button butDefault;
        protected CheckBox CBAdd;
        protected DropDownList DDLEndH;
        protected DropDownList DDLEndM;
        protected DropDownList DDLStartH;
        protected DropDownList DDLStartM;
        protected DropDownList DDLStudyDoc;
        protected DropDownList DDLType;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected int intID = 0;
        private PublicFun publicFun_0 = new PublicFun();
        protected RadioButtonList RBLStudyType;
        protected ScriptManager SM;
        protected string strReUserCode = "";
        protected string strReUserName = "";
        protected string strWorkCode = "";
        protected string strWorkName = "";
        protected TextBox txtEndDate;
        protected TextBox txtLongTime;
        protected TextBox txtMemberFen;
        protected TextBox txtPlanMemo;
        protected TextBox txtPlanName;
        protected TextBox txtStartDate;
        protected TextBox txtStudyFen;
        protected UpdatePanel UPPart;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            int num;
            string str = "";
            string str2 = "";
            string str3 = "N";
            str = base.Request.Form["txtBrowUserCode"].ToString();
            str2 = base.Request.Form["txtBrowUserName"].ToString();
            if (!this.CBAdd.Checked)
            {
                str3 = "N";
            }
            else
            {
                str3 = "Y";
            }
            string str5 = "";
            base.Request.Form["txtWorkCode"].ToString();
            str5 = base.Request.Form["txtWorkName"].ToString();
            if (this.txtStudyFen.Text.Trim() == "")
            {
                this.txtStudyFen.Text = "0";
            }
            if (this.txtMemberFen.Text.Trim() == "")
            {
                this.txtMemberFen.Text = "0";
            }
            Convert.ToDouble(this.txtLongTime.Text.Trim());
            string str6 = this.txtStartDate.Text.Trim() + " " + this.DDLStartH.SelectedItem.Value + ":" + this.DDLStartM.SelectedItem.Value + ":00";
            string str7 = this.txtEndDate.Text.Trim() + " " + this.DDLEndH.SelectedItem.Value + ":" + this.DDLEndM.SelectedItem.Value + ":00";
            string strSql = "";
            if (this.intID == 0)
            {
                strSql = "insert into StudyPlanInfo(IsAdd,LastWork,FenValue,StudyFen,PlanName,PlanMemo,StudyUserCode,StudyUserName,StudyType,StudyDocID,StudyLongTime,PlanStartTime,PlanEndTime,InputMan) values(";
                strSql = strSql + "'" + str3 + "','," + str5 + ",','" + this.txtMemberFen.Text.Trim() + "','" + this.txtStudyFen.Text.Trim() + "','" + this.txtPlanName.Text.Trim() + "','" + this.txtPlanMemo.Text.Trim().Replace("'", "‘") + "','," + str + ",','," + str2 + ",','" + this.RBLStudyType.SelectedItem.Value + "'," + this.DDLStudyDoc.SelectedItem.Value + "," + this.txtLongTime.Text.Trim() + ",";
                strSql = strSql + "'" + str6.Trim() + "','" + str7.Trim() + "','" + this.httpCookie_0.Value + "')";
                this.intID = this.publicFun_0.RetrunExecute(strSql);
                if (str.IndexOf("所有人员") <= -1)
                {
                    string[] strArray = str.Split(new char[] { ',' });
                    for (num = 0; num < strArray.Length; num++)
                    {
                        if (strArray[num].Trim() != "")
                        {
                            strSql = string.Concat(new object[] { "insert into StudyUser(UserCode,PlanID,PlanName,CountTime,LoginIP) values('", strArray[num].Trim(), "',", this.intID, ",'", this.txtPlanName.Text.Trim(), "',0,'')" });
                            this.publicFun_0.ExecuteSql(strSql);
                        }
                    }
                }
                else
                {
                    strSql = string.Concat(new object[] { "insert into StudyUser(UserCode,PlanID,PlanName,CountTime,LoginIP) SELECT T.UserCode,T.PlanID,T.PlanName,T.CountTime,T.LoginIP FROM (SELECT A.UserCode,", this.intID, " as PlanID,'", this.txtPlanName.Text.Trim(), "' as PlanName,0 as CountTime,'' as LoginIP,row_number() OVER(PARTITION BY A.UserCode order by A.UserCode) AS GROUP_IDX FROM UserInfo A) T WHERE GROUP_IDX = 1" });
                    this.publicFun_0.ExecuteSql(strSql);
                }
            }
            else
            {
                string[] strArray5 = str.Split(new char[] { ',' });
                str2.Split(new char[] { ',' });
                string[] strArray7 = this.ViewState["UserName"].ToString().Split(new char[] { ',' });
                string[] strArray6 = this.ViewState["UserCode"].ToString().Split(new char[] { ',' });
                string str9 = "," + str + ",";
                string str10 = "," + str2 + ",";
                string str8 = this.ViewState["UserCode"].ToString();
                string str11 = this.ViewState["UserName"].ToString();
                str8 = "," + str8 + ",";
                str11 = "," + str11 + ",";
                for (int i = 0; i < strArray5.Length; i++)
                {
                    for (int j = 0; j < strArray6.Length; j++)
                    {
                        if (strArray5[i].Trim() == strArray6[j].Trim())
                        {
                            str9 = str9.Replace("," + strArray6[j].Trim() + ",", ",");
                            str10 = str10.Replace("," + strArray7[j].Trim() + ",", ",");
                            str8 = str8.Replace("," + strArray6[j].Trim() + ",", ",");
                            str11 = str11.Replace("," + strArray7[j].Trim() + ",", ",");
                        }
                    }
                }
                strSql = "update StudyPlanInfo set  IsAdd='" + str3 + "',LastWork='," + str5 + ",',FenValue='" + this.txtMemberFen.Text.Trim() + "',StudyFen='" + this.txtStudyFen.Text.Trim() + "',PlanName='" + this.txtPlanName.Text.Trim() + "',PlanMemo='" + this.txtPlanMemo.Text.Trim().Replace("'", "‘") + "',StudyUserCode='," + str + ",',StudyUserName='," + str2 + ",',";
                strSql = strSql + "StudyType='" + this.RBLStudyType.SelectedItem.Value + "',StudyDocID=" + this.DDLStudyDoc.SelectedItem.Value + ",StudyLongTime=" + this.txtLongTime.Text.Trim() + ",";
                strSql = string.Concat(new object[] { strSql, "PlanStartTime='", str6, "',PlanEndTime='", str7, "',InputMan='", this.httpCookie_0.Value, "',InputTime='", DateTime.Now.ToString(), "' where ID=", this.intID });
                this.publicFun_0.ExecuteSql(strSql);
                if (str.IndexOf("所有人员") <= -1)
                {
                    string[] strArray4 = str9.Split(new char[] { ',' });
                    for (num = 0; num < strArray4.Length; num++)
                    {
                        if (strArray4[num].Trim() != "")
                        {
                            strSql = string.Concat(new object[] { "insert into StudyUser(UserCode,PlanID,PlanName,CountTime,LoginIP) values('", strArray4[num].Trim(), "',", this.intID, ",'", this.txtPlanName.Text.Trim(), "',0,'')" });
                            this.publicFun_0.ExecuteSql(strSql);
                        }
                    }
                    string[] strArray3 = str8.Split(new char[] { ',' });
                    for (num = 0; num < strArray3.Length; num++)
                    {
                        if (strArray3[num].Trim() != "")
                        {
                            strSql = string.Concat(new object[] { "delete UserStudyFen where UserCode='", strArray3[num].Trim(), "' and FormID=", this.intID, " and ExamOrStudy='S';delete from SysTip where Formtable='StudyPlanInfo' and GetNumber='", strArray3[num].Trim(), "' and FormID=", this.intID, ";delete from StudyUser where UserCode='", strArray3[num].Trim(), "' and PlanID=", this.intID });
                            this.publicFun_0.ExecuteSql(strSql);
                        }
                    }
                }
                else
                {
                    strSql = string.Concat(new object[] { "insert into StudyUser(UserCode,PlanID,PlanName,CountTime,LoginIP) select UserCode,", this.intID, ",'", this.txtPlanName.Text.Trim(), "',0,'' from UserInfo where Flag='A' and UserCode not in(select UserCode from StudyUser where PlanID=", this.intID, ")" });
                    this.publicFun_0.ExecuteSql(strSql);
                }
            }
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BEditSucc", "alert('提交学习计划任务操作成功！');parent.location.href='PlanList.aspx';WindowClosed();", true);
        }

        protected void DDLType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.method_2("0");
            this.strReUserCode = base.Request.Form["txtBrowUserCode"].ToString();
            this.strReUserName = base.Request.Form["txtBrowUserName"].ToString();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        private void method_0()
        {
            string cmdText = "select IsAdd,LastWork,FenValue,StudyFen,PlanName,PlanMemo,StudyUserCode,StudyUserName,StudyType,StudyDocID,StudyLongTime,PlanStartTime,PlanEndTime from StudyPlanInfo where ID=" + this.intID;
            if (!((this.ViewState["AddPower"].ToString() == "Y") ? !(this.ViewState["ManagPower"].ToString() == "N") : true))
            {
                cmdText = cmdText + " and InputMan='" + this.httpCookie_0.Value + "'";
            }
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(cmdText, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            try
            {
                if (reader.Read())
                {
                    this.txtMemberFen.Text = reader["FenValue"].ToString();
                    this.txtStudyFen.Text = reader["StudyFen"].ToString();
                    this.txtStartDate.Text = reader["PlanStartTime"].ToString().Substring(0, reader["PlanStartTime"].ToString().LastIndexOf(" "));
                    this.txtEndDate.Text = reader["PlanEndTime"].ToString().Substring(0, reader["PlanEndTime"].ToString().LastIndexOf(" "));
                    this.DDLStartH.SelectedIndex = -1;
                    this.DDLStartM.SelectedIndex = -1;
                    this.DDLEndH.SelectedIndex = -1;
                    this.DDLEndM.SelectedIndex = -1;
                    this.DDLStartH.Items.FindByValue(Convert.ToDateTime(reader["PlanStartTime"]).Hour.ToString()).Selected = true;
                    this.DDLStartM.Items.FindByValue(reader["PlanStartTime"].ToString().Substring(reader["PlanStartTime"].ToString().IndexOf(":") + 1, 2)).Selected = true;
                    this.DDLEndH.Items.FindByValue(Convert.ToDateTime(reader["PlanEndTime"]).Hour.ToString()).Selected = true;
                    this.DDLEndM.Items.FindByValue(reader["PlanEndTime"].ToString().Substring(reader["PlanEndTime"].ToString().IndexOf(":") + 1, 2)).Selected = true;
                    this.txtPlanName.Text = reader["PlanName"].ToString();
                    this.txtLongTime.Text = reader["StudyLongTime"].ToString();
                    this.txtPlanMemo.Text = reader["PlanMemo"].ToString();
                    this.RBLStudyType.SelectedIndex = -1;
                    this.DDLStudyDoc.SelectedIndex = -1;
                    this.DDLType.SelectedIndex = -1;
                    this.RBLStudyType.Items.FindByValue(reader["StudyType"].ToString()).Selected = true;
                    this.method_1(reader["StudyDocID"].ToString());
                    this.strReUserName = reader["StudyUserName"].ToString().Substring(1, reader["StudyUserName"].ToString().Length - 2);
                    this.strReUserCode = reader["StudyUserCode"].ToString().Substring(1, reader["StudyUserCode"].ToString().Length - 2);
                    this.ViewState["UserName"] = this.strReUserName;
                    this.ViewState["UserCode"] = this.strReUserCode;
                    this.strWorkName = reader["LastWork"].ToString().Substring(1, reader["LastWork"].ToString().Length - 2);
                    this.strWorkCode = reader["LastWork"].ToString().Substring(1, reader["LastWork"].ToString().Length - 2);
                    this.DDLStudyDoc.Items.FindByValue(reader["StudyDocID"].ToString()).Selected = true;
                    if (!(reader["IsAdd"].ToString().Trim() == "Y"))
                    {
                        this.CBAdd.Checked = false;
                    }
                    else
                    {
                        this.CBAdd.Checked = true;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
        }

        private void method_1(string string_0)
        {
            if (this.RBLStudyType.SelectedItem.Value != "A")
            {
                if (this.ViewState["AddPower"].ToString() == "Y")
                {
                    this.publicFun_0.GetType(this.DDLType, "LessionType", this.httpCookie_0.Value, "B", this.httpCookie_3.Value);
                }
                if (this.ViewState["ManagPower"].ToString() == "Y")
                {
                    this.publicFun_0.GetType(this.DDLType, "LessionType", this.httpCookie_0.Value, "Y", this.httpCookie_3.Value);
                }
            }
            else
            {
                if (this.ViewState["AddPower"].ToString() == "Y")
                {
                    this.publicFun_0.GetType(this.DDLType, "BookType", this.httpCookie_0.Value, "B", this.httpCookie_3.Value);
                }
                if (this.ViewState["ManagPower"].ToString() == "Y")
                {
                    this.publicFun_0.GetType(this.DDLType, "BookType", this.httpCookie_0.Value, "Y", this.httpCookie_3.Value);
                }
            }
            this.method_2(string_0);
        }

        private void method_2(string string_0)
        {
            try
            {
                string values;
                if (this.RBLStudyType.SelectedItem.Value != "A")
                {
                    if (string_0.Trim() != "0")
                    {
                        values = this.publicFun_0.GetValues("select LessionTypeID from LessionInfo where ID=" + string_0, "LessionTypeID");
                        this.DDLType.SelectedIndex = -1;
                        this.DDLType.Items.FindByValue(values).Selected = true;
                    }
                    if (this.DDLType.SelectedIndex != -1)
                    {
                        this.DDLStudyDoc.DataTextField = "LessionName";
                        this.DDLStudyDoc.DataValueField = "ID";
                        this.DDLStudyDoc.DataSource = this.publicFun_0.RetrunDataView("select ID,LessionName from LessionInfo where LessionTypeID=" + this.DDLType.SelectedItem.Value + " order by SortInfo,ID");
                        this.DDLStudyDoc.DataBind();
                        if (string_0 != "0")
                        {
                            this.DDLStudyDoc.SelectedIndex = -1;
                            this.DDLStudyDoc.Items.FindByValue(string_0).Selected = true;
                        }
                    }
                }
                else
                {
                    if (string_0.Trim() != "0")
                    {
                        values = this.publicFun_0.GetValues("select SubjectTypeID from BookInfo where ID=" + string_0, "SubjectTypeID");
                        this.DDLType.SelectedIndex = -1;
                        this.DDLType.Items.FindByValue(values).Selected = true;
                    }
                    if (this.DDLType.SelectedIndex != -1)
                    {
                        this.DDLStudyDoc.DataTextField = "SubjectName";
                        this.DDLStudyDoc.DataValueField = "ID";
                        this.DDLStudyDoc.DataSource = this.publicFun_0.RetrunDataView("select ID,SubjectName from BookInfo where SubjectTypeID=" + this.DDLType.SelectedItem.Value + " order by SortInfo,ID");
                        this.DDLStudyDoc.DataBind();
                        if (string_0 != "0")
                        {
                            this.DDLStudyDoc.SelectedIndex = -1;
                            this.DDLStudyDoc.Items.FindByValue(string_0).Selected = true;
                        }
                    }
                }
            }
            catch
            {
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.intID = Convert.ToInt32(base.Request["ID"].ToString());
                base.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString + "---定制学习任务";
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) == 0x3a)
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTHB", "alert('该版本为特惠版，此功能未开放！')", true);
                }
                else if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddStudyPlan,ManagStudyPlan from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddStudyPlan"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagStudyPlan"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.txtStartDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                        this.txtEndDate.Text = DateTime.Now.AddDays(30.0).ToString("yyyy-MM-dd");
                        if ((this.ViewState["AddPower"].ToString() == "N") ? !(this.ViewState["ManagPower"].ToString() == "N") : true)
                        {
                            this.butDefault.Visible = true;
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('添加或管理学习任务权限未开放！')", true);
                            this.butDefault.Visible = false;
                        }
                        if (this.intID != 0)
                        {
                            this.method_0();
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }

        protected void RBLStudyType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.method_1("0");
            this.strReUserCode = base.Request.Form["txtBrowUserCode"].ToString();
            this.strReUserName = base.Request.Form["txtBrowUserName"].ToString();
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }
    }
}

